<?php

namespace app\common\model\Product;

use think\Model;

/**
 * 产品模型
 */
class Product extends Model
{
  // 设置当前模型对应的完整数据表名称
  protected $table = 'pre_product';

  // 开启自动写入时间戳字段
  protected $autoWriteTimestamp = true;

  // 定义时间戳字段名
  protected $createTime = 'createtime';

  // 关闭自动写入update_time字段
  protected $updateTime = false;


  protected $append = [
    'thumbs_text',
    'thumbs_list',
    'content_text', // 文章内容（去除掉标签）
  ];

  public function getThumbsTextAttr($value, $data)
  {
    $url = config('site.url');

    if (!empty($data['thumbs'])) {
      $thumb = explode(',', $data['thumbs']);
      return $url . $thumb[0];
    } else {
      return $url . '/assets/img/product.jpg';
    }
  }

  public function getThumbsListAttr($value, $data)
  {
    $url = config('site.url');

    $thumb = '';

    if (!empty($data['thumbs'])) {
      $thumb =  str_replace('/uploads', $url . '/uploads', $data['thumbs']);

      $thumb = explode(',', $thumb);
    } else {

      $thumb = [$url . '/assets/img/product.jpg'];
    }

    return $thumb;
  }

  // 格式描述内容
  public function getContentTextAttr($value, $data)
  {
    // $content = !empty($data['content']) ? $data['content'] : '';
    return strip_tags($data['content']);
  }
}
